/*
 *
 * Copyright 2014-2025 Florian Schmaus
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.smack;

import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.SmackException.NotLoggedInException;
import org.jivesoftware.smack.util.Objects;

/**
 * Managers provide the high-level API of certain functionality (often standardized by XMPP Extension Protocols).
 */
public abstract class Manager {

    private static final Logger LOGGER = Logger.getLogger(Manager.class.getName());

    final WeakReference<XMPPConnection> weakConnection;

    public Manager(XMPPConnection connection) {
        Objects.requireNonNull(connection, "XMPPConnection must not be null");

        weakConnection = new WeakReference<>(connection);
    }

    protected final XMPPConnection connection() {
        return weakConnection.get();
    }

    /**
     * Get the XMPPConnection of this Manager if it's authenticated, i.e. logged in. Otherwise throw a {@link NotLoggedInException}.
     *
     * @return the XMPPConnection of this Manager.
     * @throws NotLoggedInException if the connection is not authenticated.
     */
    protected final XMPPConnection getAuthenticatedConnectionOrThrow() throws NotLoggedInException {
        XMPPConnection connection = connection();
        if (!connection.isAuthenticated()) {
            throw new NotLoggedInException();
        }
        return connection;
    }

    protected static final ScheduledAction schedule(Runnable runnable, long delay, TimeUnit unit) {
        return schedule(runnable, delay, unit, ScheduledAction.Kind.NonBlocking);
    }

    protected static final ScheduledAction scheduleBlocking(Runnable runnable, long delay, TimeUnit unit) {
        return schedule(runnable, delay, unit, ScheduledAction.Kind.Blocking);
    }

    protected static final ScheduledAction schedule(Runnable runnable, long delay, TimeUnit unit, ScheduledAction.Kind scheduledActionKind) {
        return AbstractXMPPConnection.SMACK_REACTOR.schedule(runnable, delay, unit, scheduledActionKind);
    }

    protected interface WithConnection {
        void run(XMPPConnection connection) throws NotConnectedException, InterruptedException;
    }

    protected void ignoreDisconnected(WithConnection withConnection) throws InterruptedException {
        var connection = connection();

        // Early return to avoid potential expensive withConnection closures.
        if (!connection.isConnected()) return;

        try {
            withConnection.run(connection);
        } catch (NotConnectedException e) {
            LOGGER.log(Level.FINER, "Ignoring " + e, e);
        }
    }
}
